package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportPurchaseInventoryDetailRpcService;
import com.hunttown.mes.rpc.domain.TransportPurchaseInventoryDetailDTO;
import com.hunttown.mes.rpc.domain.query.TransportPurchaseInventoryDetailDTOQuery;
import com.hunttown.mes.service.TransportPurchaseInventoryDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportPurchaseInventoryDetailConverts.convert;

/**
 * created by wangjunfu on 2019-12-24 15:52:04
 */
@Service
public class TransportPurchaseInventoryDetailRpcServiceImpl implements TransportPurchaseInventoryDetailRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportPurchaseInventoryDetailRpcServiceImpl.class);

    private final TransportPurchaseInventoryDetailService transportPurchaseInventoryDetailService;

    @Autowired
    public TransportPurchaseInventoryDetailRpcServiceImpl(TransportPurchaseInventoryDetailService transportPurchaseInventoryDetailService) {
        this.transportPurchaseInventoryDetailService = transportPurchaseInventoryDetailService;
    }

    @Override
    public TransportPurchaseInventoryDetailDTO insert(TransportPurchaseInventoryDetailDTO transportPurchaseInventoryDetailDTO) {
        try {
            return convert(transportPurchaseInventoryDetailService.insert(convert(transportPurchaseInventoryDetailDTO)));
        } catch (Exception e) {
            logger.error("TransportPurchaseInventoryDetail create failed.", e);
            throw new RuntimeException("TransportPurchaseInventoryDetail create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportPurchaseInventoryDetailDTO transportPurchaseInventoryDetailDTO) {
        try {
            return transportPurchaseInventoryDetailService.updateInfoById(convert(transportPurchaseInventoryDetailDTO));
        } catch (Exception e) {
            logger.error("TransportPurchaseInventoryDetail update by id failed.", e);
            throw new RuntimeException("TransportPurchaseInventoryDetail update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportPurchaseInventoryDetailDTOQuery transportPurchaseInventoryDetailDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportPurchaseInventoryDetailDTOQuery.getW_id() != null && transportPurchaseInventoryDetailDTOQuery.getW_id() > 0) {
                query.put("w_id", transportPurchaseInventoryDetailDTOQuery.getW_id());
            }

            if (transportPurchaseInventoryDetailDTOQuery.getIds() != null && !transportPurchaseInventoryDetailDTOQuery.getIds().equals("")) {
                query.put("ids", transportPurchaseInventoryDetailDTOQuery.getIds());
            }

            if (transportPurchaseInventoryDetailDTOQuery.getPartIds() != null && !transportPurchaseInventoryDetailDTOQuery.getPartIds().equals("")) {
                query.put("partIds", transportPurchaseInventoryDetailDTOQuery.getPartIds());
            }

            if (transportPurchaseInventoryDetailDTOQuery.getW_inventoryId() != null) {
                query.put("w_inventoryId", transportPurchaseInventoryDetailDTOQuery.getW_inventoryId());
            }

            if (transportPurchaseInventoryDetailDTOQuery.getW_pYear() != null) {
                query.put("w_pYear", transportPurchaseInventoryDetailDTOQuery.getW_pYear());
            }

            if (transportPurchaseInventoryDetailDTOQuery.getW_pMonth() != null) {
                query.put("w_pMonth", transportPurchaseInventoryDetailDTOQuery.getW_pMonth());
            }

            if (transportPurchaseInventoryDetailDTOQuery.getInventoryId() != null) {
                query.put("inventoryId", transportPurchaseInventoryDetailDTOQuery.getInventoryId());
            }

            if (transportPurchaseInventoryDetailDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportPurchaseInventoryDetailDTOQuery.getStopFlag());
            }

            if (transportPurchaseInventoryDetailDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportPurchaseInventoryDetailDTOQuery.getDeleteFlag());
            }

            return transportPurchaseInventoryDetailService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportPurchaseInventoryDetail update by query failed.", e);
            throw new RuntimeException("TransportPurchaseInventoryDetail update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportPurchaseInventoryDetailDTO selectById(Integer id) {
        try {
            return convert(transportPurchaseInventoryDetailService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportPurchaseInventoryDetail select by id failed.", e);
            throw new RuntimeException("TransportPurchaseInventoryDetail select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportPurchaseInventoryDetailDTO selectObjByQuery(TransportPurchaseInventoryDetailDTOQuery transportPurchaseInventoryDetailDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportPurchaseInventoryDetailDTOQuery.getEntId() != null && transportPurchaseInventoryDetailDTOQuery.getEntId() > 0) {
                query.put("entId", transportPurchaseInventoryDetailDTOQuery.getEntId());
            }

            if (transportPurchaseInventoryDetailDTOQuery.getPartId() != null) {
                query.put("partId", transportPurchaseInventoryDetailDTOQuery.getPartId());
            }

            if (transportPurchaseInventoryDetailDTOQuery.getpYear() != null) {
                query.put("pYear", transportPurchaseInventoryDetailDTOQuery.getpYear());
            }

            if (transportPurchaseInventoryDetailDTOQuery.getpMonth() != null) {
                query.put("pMonth", transportPurchaseInventoryDetailDTOQuery.getpMonth());
            }

            return convert(transportPurchaseInventoryDetailService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportPurchaseInventoryDetail select obj by query failed.", e);
            throw new RuntimeException("TransportPurchaseInventoryDetail select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportPurchaseInventoryDetailDTO> getListForPage(TransportPurchaseInventoryDetailDTOQuery transportPurchaseInventoryDetailDTOQuery) {
        Query query = Query.page(transportPurchaseInventoryDetailDTOQuery.getPn(), transportPurchaseInventoryDetailDTOQuery.getPageSize());

        if (transportPurchaseInventoryDetailDTOQuery.getId() != null && transportPurchaseInventoryDetailDTOQuery.getId() > 0) {
            query.put("id", transportPurchaseInventoryDetailDTOQuery.getId());
        }

        if (transportPurchaseInventoryDetailDTOQuery.getIds() != null && !transportPurchaseInventoryDetailDTOQuery.getIds().equals("")) {
            query.put("ids", transportPurchaseInventoryDetailDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportPurchaseInventoryDetailDTOQuery.getLikeName())) {
            query.put("likeName", transportPurchaseInventoryDetailDTOQuery.getLikeName());
        }

        if (transportPurchaseInventoryDetailDTOQuery.getPurchaseType() != null) {
            query.put("purchaseType", transportPurchaseInventoryDetailDTOQuery.getPurchaseType());
        }

        if (StringUtils.isNotBlank(transportPurchaseInventoryDetailDTOQuery.getShowDate())) {
            query.put("showDate", transportPurchaseInventoryDetailDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportPurchaseInventoryDetailDTOQuery.getStartDate())) {
            query.put("startDate", transportPurchaseInventoryDetailDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportPurchaseInventoryDetailDTOQuery.getEndDate())) {
            query.put("endDate", transportPurchaseInventoryDetailDTOQuery.getEndDate());
        }

        if (transportPurchaseInventoryDetailDTOQuery.getInventoryId() != null) {
            query.put("inventoryId", transportPurchaseInventoryDetailDTOQuery.getInventoryId());
        }

        if (transportPurchaseInventoryDetailDTOQuery.getpYear() != null) {
            query.put("pYear", transportPurchaseInventoryDetailDTOQuery.getpYear());
        }

        if (transportPurchaseInventoryDetailDTOQuery.getpMonth() != null) {
            query.put("pMonth", transportPurchaseInventoryDetailDTOQuery.getpMonth());
        }

        if (StringUtils.isNotBlank(transportPurchaseInventoryDetailDTOQuery.getOrderBy())) {
            query.put("orderBy", transportPurchaseInventoryDetailDTOQuery.getOrderBy());
        }

        if (transportPurchaseInventoryDetailDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportPurchaseInventoryDetailDTOQuery.getStopFlag());
        }

        if (transportPurchaseInventoryDetailDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportPurchaseInventoryDetailDTOQuery.getDeleteFlag());
        }

        List<TransportPurchaseInventoryDetailDTO> list = convert(transportPurchaseInventoryDetailService.selectByQuery(query));
        Page<TransportPurchaseInventoryDetailDTO> page = Page.from(transportPurchaseInventoryDetailDTOQuery.getPn(), transportPurchaseInventoryDetailDTOQuery.getPageSize(), list);
        page.setTotalItems(transportPurchaseInventoryDetailService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportPurchaseInventoryDetailService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportPurchaseInventoryDetail delete failed.", e);
            throw new RuntimeException("TransportPurchaseInventoryDetail delete failed, Please order your code.", e);
        }
    }
}